<%@ page language="java" import="com.alibaba.fastjson.JSONArray" pageEncoding="UTF-8" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="okhttp3.*" %>
<%@ page import="org.apache.axis.encoding.Base64" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.util.UUID" %>
<%@ page import="java.util.concurrent.TimeUnit" %>
<%--流程查询库存量数据调用SAP查询库存接口--%>
<%
    JSONObject jsonData = new JSONObject();
    RecordSet rs = new RecordSet();
    try {
        JSONArray dataList = new JSONArray();
        String tablename = "formtable_main_2176";
        String requestid = "3972000";
        JSONObject itemMap = new JSONObject();
        rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
        rs.next();
        String[] columnName = rs.getColumnName();
        JSONObject mainMap = new JSONObject();
        for (String column : columnName) {
            String columnValue = Util.null2String(rs.getString(column));
            mainMap.put(column.toLowerCase(), columnValue);
        }
        out.println(">>>>>mainMap="+mainMap+"<br/>");
        rs.executeQuery("select loginid from hrmresource where id='" + mainMap.getString("sqrxm") + "'");
        //工号
        String loginid = "10000100";
//        if (rs.next()) {
//            loginid = rs.getString("loginid");
//        }
        out.println(">>>>>loginid="+loginid+"<br/>");

        JSONArray datasList9526 = new JSONArray();
        JSONObject jsonMap9526 = new JSONObject();
        rs.executeQuery("select * from "+tablename+"_dt1 where mainid=?",mainMap.getString("id"));
        String[] columnNamedt1 = rs.getColumnName();
        int m = 1;
        while (rs.next()){
            JSONObject map3 = new JSONObject();
            for (String column : columnNamedt1) {
                String columnValue = Util.null2String(rs.getString(column));
                map3.put(column, columnValue);
            }
            String kk = "A";
            if (map3.get("sfkk").equals("1")) {
                kk = "B";
            } else if (map3.get("sfkk").equals("2")) {
                kk = "";
            }
            jsonMap9526.put("ZZSFXM0" + m, map3.getString("sfgh"));
            jsonMap9526.put("ZZFPRQ0" + m, map3.getString("sfykssj").replaceAll("-", ""));
            jsonMap9526.put("ZZZZRQ0" + m, map3.getString("sfyjssj").replaceAll("-", ""));
            jsonMap9526.put("ZZSFFS0" + m, map3.getString("sdtfs"));
            jsonMap9526.put("ZZSFKK0" + m, kk);
            jsonMap9526.put("ZZTMZT0" + m, map3.getString("1"));
            m = m + 1;
        }
        if (jsonMap9526.size() > 0) {
            String yq = "";
            if (mainMap.get("sfyqzz").equals("0")) {
                yq = "X";
            }
            jsonMap9526.put("BEGDA", mainMap.getString("zzrq").replaceAll("-", ""));
            jsonMap9526.put("ENDDA", "99991231");
            jsonMap9526.put("ZZPLANS", mainMap.getString("gwsdt"));
            jsonMap9526.put("ZZSFYQ", yq);
            datasList9526.add(jsonMap9526);
        }
        itemMap.put("P9526", datasList9526);
        itemMap.put("PERNR", loginid);
        itemMap.put("MASSN", "Z3");
        itemMap.put("MASSG", mainMap.getString("khlb"));
        itemMap.put("BEGDA", mainMap.getString("zzrq").replaceAll("-", ""));
        itemMap.put("ENDDA", "99991231");
        itemMap.put("ZZGWZT", mainMap.getString("gwzt"));
        itemMap.put("ZZSYZT", mainMap.getString("syqzt"));
        //新增劳动合同归属ZZGSRS
        itemMap.put("ZZGSRS", mainMap.getString("ldhtgs"));
        //210924增加TCL人员类别、TCL岗位类别、人员是否归属在建成本、人员费用归属、外派标识及备注1-5
        itemMap.put("ZZTCLRYLB", mainMap.getString("tclrylb"));
        itemMap.put("ZZTCLGWLB", mainMap.getString("tclgwlb"));
        itemMap.put("ZZZJCBBS", mainMap.getString("sfgszjcb"));
        itemMap.put("ZZRYFYGS", mainMap.getString("ryfygs"));
        itemMap.put("ZZWPBS", mainMap.getString("wpbs"));
        itemMap.put("ZZBZ1", mainMap.getString("bz1"));
        itemMap.put("ZZBZ2", mainMap.getString("bz2"));
        itemMap.put("ZZBZ3", mainMap.getString("bz3"));
        itemMap.put("ZZBZ4", mainMap.getString("bz4"));
        itemMap.put("ZZBZ5", mainMap.getString("bz5"));
        dataList.add(itemMap);
        out.println(">>>>>dataList="+dataList+"<br/>");
        JSONObject resultJson = handleOKHttpRequest("EC112", dataList);
        jsonData.put("resultJson", resultJson);
    } catch (Exception e) {
        e.printStackTrace();
    }
    out.print(jsonData.toJSONString());
%>
<%!
    private static BaseBean baseBean = new BaseBean();
    private static final String URL = "http://10.48.44.179:50000/RESTAdapter/ecthcm/interface";
    private static final String USERNAME = "po_user";
    private static final String PASSWORD = "Init123456!";

    private static JSONObject handleOKHttpRequest(String type, JSONArray dataJson) {
        JSONObject paramsJson = new JSONObject();
        JSONObject inuptJson = new JSONObject();
        String fullDate = DateUtil.getFullDate();
        String curdate = fullDate.substring(0, 10).replace("-", "");
        String curtime = fullDate.substring(11, 19).replace(":", "");
        JSONObject headerJson = new JSONObject();
        headerJson.put("SYSID", "EC");
        headerJson.put("BUSINESS", type);
        headerJson.put("BUSNKEY", "YEJAJFHJDHKA");
        headerJson.put("TECHKEY", getUUIDStr());
        headerJson.put("DATE", curdate);
        headerJson.put("TIME", curtime);
        inuptJson.put("HEADER", headerJson);
        inuptJson.put("DATA", dataJson);
        paramsJson.put("INPUT", inuptJson.toJSONString());
        new BaseBean().writeLog("handleOKHttpRequest>>>>paramsJson="+paramsJson);
        return sendOKHttpRequest(paramsJson);
    }

    private static JSONObject sendOKHttpRequest(JSONObject params) {
        JSONObject resultJson = new JSONObject();
        String code = "fail";
        String message = "";
        String data = "";
        try {
            String authorization = " Basic " + Base64.encode((USERNAME + ":" + PASSWORD).getBytes());
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, params.toJSONString());
            Request request = new Request.Builder()
                    .url(URL)
                    .method("POST", body)
                    .addHeader("Authorization", authorization)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();
            data = Util.null2String(response.body().string());
            if (!"".equals(data)) {
                resultJson.put("data", JSONObject.parseObject(data));
                code = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            e.printStackTrace();
        }
        resultJson.put("code", code);
        resultJson.put("message", message);
        return resultJson;
    }

    private static String getUUIDStr() {
        String uuid = UUID.randomUUID().toString();
        return uuid.replace("-", "");
    }

%>



